package com.sirius.sipms.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class AppContextUtil implements ApplicationContextAware {
	private static ApplicationContext context;
	public static ApplicationContext getContext() {
		return context;
	}

	public static void setContext(ApplicationContext context) {
		AppContextUtil.context = context;
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		context = applicationContext;
	}
	
	/**
	 * 根据bean名称获取object
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		if(getContext().containsBean(beanName)) {
			return getContext().getBean(beanName);
		} else {
			System.out.println("您配置的service:["+beanName+"]不存在");
			return null;
		}
	}
}
